<?php

namespace Luka\WindowBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class PageController extends WindowBaseController
{
    public function indexAction() {
        return array();
    }

    public function showAction($id) {
        $em = $this->getDoctrine()->getEntityManager();
        $this->GenerateMenu($em);
        //echo "id: ".$id."<br>";
        return $this->render('LukaWindowBundle:Home:index.html.twig');
    }

    public function showObjectAction($id) {
        $query = $this->entityManager->createQuery('
            SELECT p.id, p.title, p.body, o.id as object_id, ot.id as objecttype_id FROM LukaStoreBundle:Page p
            JOIN p.object o
            JOIN o.objecttype ot
            WHERE p.visibility = :visibility
            AND p.id = :id
            AND p.language = :lang
            ORDER BY p.ordernumber
        ')
            ->setParameters(
            array(
                "visibility" => "1",
                "id" => $id,
                "lang" => $this->locale
            )
        );

        $result = $query->getArrayResult();
        $title = $body = $this->get('translator')->trans('Objekti ei leitud!');
        $objectId = null;

        if(count($result) != 0){
            $result = current($result);
            $title = $result["title"];
            $body = $result["body"];
            $objectId = $result["object_id"];
        }

        $paginator = $this->Pagenator('LukaStoreBundle:Page', 'p', array("id", "linktitle"), array('visibility' => '1', "language" => $this->locale, "parent" => $id), "LukaWindowBundle:Home:objectListItem.html.twig");

        return $this->render('LukaWindowBundle:Home:object.html.twig', array_merge(array(
            "title" => $title,
            "body" => $body),
            $paginator
        ));
    }
}
